package com.waghost.bridalgown.controller;

import com.waghost.bridalgown.entity.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
// 文件管理
@RestController
@CrossOrigin
public class FileController {
    @Value("${filePath}")
    private String filePath;

    @GetMapping("/download/{filePath}")
    public void upload(@PathVariable("filePath")String path, HttpServletResponse response) {
        if (path == null){
            return;
        }
        InputStream inputStream = null;
        ServletOutputStream fileOutputStream = null;
        try {
            inputStream = new FileInputStream(filePath+"/"+path);
            fileOutputStream = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int index = 0;
            while ((index = inputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, index);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        System.out.println(filePath + "/" + file.getOriginalFilename());
        try {
            inputStream = file.getInputStream();
            String path = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            fileOutputStream = new FileOutputStream(filePath + "/" + path);
            byte[] bytes = new byte[1024];
            int index = 0;
            while ((index = inputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, index);
            }
            return  Result.success(path);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return  Result.success(null,"上传失败",500);
    }
}
